package com.facedemo.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.facedemo.MyApplication;
import com.facedemo.R;
import com.facedemo.bean.FaceFeature;
import com.facedemo.bean.RegisterFeatureInfo;
import com.facedemo.db.entity.Person;
import com.facedemo.db.entity.PersonFeature;
import com.facedemo.db.utils.PersonDaoUtils;
import com.facedemo.db.utils.PersonFeatureDaoUtils;
import com.facedemo.face.FaceInfoManger;
import com.facedemo.face.FaceRegisterUtil;
import com.facedemo.face.callback.FaceRegisterCallback;
import com.facedemo.utils.ImageUtils;
import com.facedemo.utils.SoundPoolUtils;
import com.facedemo.view.CameraSurface;
import com.facedemo.view.FaceMarkSurface;

import java.util.ArrayList;
import java.util.List;

//人脸注册
public class FaceRegisterActivity extends Activity {
    private CameraSurface mCameraSurfaceView;
    private FaceMarkSurface surface_draw;
    private TextView tv_name,tv_tip;
    private ImageView iv_face1,iv_face2,iv_face3;
    private Button btn_back;

    private FaceRegisterUtil faceRegisterUtil;
    private String personId;
    private String mName;
    //每人注册3 张人脸
    private List<RegisterFeatureInfo> faceList = new ArrayList<>();

    private SoundPoolUtils soundPoolUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_register);
        initView();

        soundPoolUtils = new SoundPoolUtils(FaceRegisterActivity.this,new int[]{R.raw.finish_register,R.raw.start_register,
                R.raw.zheng_shi,R.raw.left,R.raw.right});

        personId = getIntent().getStringExtra("personId");
        mName = getIntent().getStringExtra("name");
        tv_name.setText(mName);

        faceRegisterUtil = new FaceRegisterUtil(mCameraSurfaceView,surface_draw);
        faceRegisterUtil.setFaceCallback(callback);

        tv_tip.setText("开始人脸注册，请正视摄像头");
        soundPoolUtils.playSound(R.raw.start_register);
        myHandler.sendEmptyMessageDelayed(0x80,1500);
    }

    private void initView() {
        mCameraSurfaceView = findViewById(R.id.surface_camera);
        surface_draw = findViewById(R.id.surface_draw);
        tv_name = findViewById(R.id.tv_name);
        tv_tip = findViewById(R.id.tv_tip);
        iv_face1 = findViewById(R.id.iv_face1);
        iv_face2 = findViewById(R.id.iv_face2);
        iv_face3 = findViewById(R.id.iv_face3);
        btn_back = findViewById(R.id.btn_back);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private FaceRegisterCallback callback = new FaceRegisterCallback() {
        @Override
        public void success(Bitmap faceBitmap, byte[] feature) {
            RegisterFeatureInfo info = new RegisterFeatureInfo();
            info.setFaceBitmap(faceBitmap);
            info.setFeature(feature);
            faceList.add(info);

            if(faceList.size() == 1) {
                //去注册第二张人脸
                myHandler.sendEmptyMessage(0x81);
            } else if(faceList.size() == 2) {
                //去注册第三张人脸
                myHandler.sendEmptyMessage(0x82);
            } else if(faceList.size() == 3) {
                //完成注册，保存人脸信息

                //保存人员到数据库
                Person person = new Person();
                person.setPersonId(personId);
                person.setName(mName);
                PersonDaoUtils.addOrUpdate(person);

                for(int i=0;i<faceList.size();i++) {
                    int faceId = i+1;

                    //保存图片
                    String path = MyApplication.DIR_PATH + personId + "_" + faceId + ".jpg";
                    ImageUtils.save(faceBitmap,path,Bitmap.CompressFormat.JPEG,false);

                    //保存人脸到数据库
                    PersonFeature pf = new PersonFeature();
                    pf.setPersonId(personId);
                    pf.setFaceId(i+1);
                    pf.setFeature(faceList.get(i).getFeature());
                    pf.setFacePicPath(path);
                    PersonFeatureDaoUtils.addOrUpdate(pf);

                    //添加人脸到人脸库
                    FaceFeature ff = new FaceFeature();
                    ff.personId = personId;
                    ff.faceId = faceId;
                    ff.feature = faceList.get(i).getFeature();
                    FaceInfoManger.getInstance().addOrUpdateFace(ff);
                }

                myHandler.sendEmptyMessage(0x83);
            }
        }
    };

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x80:
                    faceRegisterUtil.startGetFace();
                    break;
                case 0x81:
                    iv_face1.setImageBitmap(faceList.get(0).getFaceBitmap());
                    soundPoolUtils.playSound(R.raw.left);
                    tv_tip.setText("请轻微左转头");
                    myHandler.sendEmptyMessageDelayed(0x80,2000);
                    break;
                case 0x82:
                    iv_face2.setImageBitmap(faceList.get(1).getFaceBitmap());
                    soundPoolUtils.playSound(R.raw.right);
                    tv_tip.setText("请轻微右转头");
                    myHandler.sendEmptyMessageDelayed(0x80,2000);
                    break;
                case 0x83:
                    iv_face3.setImageBitmap(faceList.get(2).getFaceBitmap());
                    soundPoolUtils.playSound(R.raw.finish_register);
                    tv_tip.setText("恭喜你完成注册!");
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(myHandler!=null) {
            myHandler.removeCallbacksAndMessages(null);
            myHandler = null;
        }
    }
}
